package io.sdl.controller;


import io.sdl.common.sys.entity.SdlDoctorsEntity;
import io.sdl.common.sys.service.SdlDoctorsService;
import io.sdl.common.sys.vo.SdlDoctorsVo;
import io.sdl.common.utils.Result;
import io.sdl.common.constant.UrlConstants;
import io.sdl.dto.DictDTO;
import io.sdl.service.SysDictDataService;
import io.sdl.service.UserService;
import io.sdl.utils.StringHandlsUtils;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author Dc 1211734836@qq.com
 * @since 1.0.0 2025-03-28
 */
@RestController
@RequestMapping("/api")
@Tag(name = "")
public class ApiSdlDoctorsController {

	@Autowired
	private SdlDoctorsService sdlDoctorsService;

	@Autowired
	private SysDictDataService sysDictDataService;

	@Autowired
	private UserService userService;

	@GetMapping("find/doctor")
	public Result findAllDoctor(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {

		//机构类型筛选
		if (params.get("technologyType").equals("全部")) {
			params.put("technologyType", null);

		} else {
			DictDTO dictValue = sysDictDataService.getDictValue("technologyType", (String) params.get("technologyType"));
			params.put("technologyType", dictValue.getDictValue());
		}
		params.put("page", Integer.valueOf((String) params.get("page")) - 1);
		params.put("pageSize", Integer.valueOf((String) params.get("pageSize")));
		List<SdlDoctorsVo> all = sdlDoctorsService.findAll(params);
		if (params.get("sort").equals("评分最高")) {
			all = all.stream().sorted(Comparator.comparing(SdlDoctorsVo::getDoctorsProevalution).reversed()).collect(Collectors.toList());
		}
		all.forEach(a -> {
			a.setAvatarUrl(UrlConstants.getProductionUrl() + a.getAvatarUrl());
			String[] split = a.getTechnologyType().split(",");
			String technologyTypes = "";
			for (String s : split) {
				String technologyType = sysDictDataService.getLabel("technologyType", s).getDictLabel();
				technologyTypes += technologyType + "、";
			}
			a.setTechnologyType(StringHandlsUtils.trimComma(technologyTypes));
		});
		return new Result().ok(all);
	}


	@GetMapping("doctor/{id}")
	public Result getDoctorDetail(@PathVariable("id") Long id) {

		SdlDoctorsVo detail = sdlDoctorsService.getDetail(id);
		detail.setAvatarUrl(UrlConstants.getProductionUrl() + detail.getAvatarUrl());
		String[] split = detail.getTechnologyType().split(",");
		String technologyTypes = "";
		for (String s : split) {
			String technologyType = sysDictDataService.getLabel("technologyType", s).getDictLabel();
			technologyTypes += technologyType + "、";
		}
		detail.setTechnologyType(StringHandlsUtils.trimComma(technologyTypes));
		return new Result().ok(detail);
	}


}
